package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author 20201117
 */
@Controller
@RequestMapping("/wcart")
public class CartController {

    @Autowired
    private CartFeign cartFeign;

    /**
     * 查询 购物车列表
     * @return
     */
    @GetMapping("/list")
    public String cartList(Model model){
        Map data = cartFeign.list().getData();
        model.addAttribute("items",data);
        return "cart";
    }


    /**
     * 添加 商品 到 购物车
     * @param skuId
     * @param num
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result<Map> add(String skuId, Integer num){
        //添加 购物车 信息
        cartFeign.add(skuId,num);
        // 添加成功之后， 刷新 购物车列表信息
        Map map = cartFeign.list().getData();
        return new Result(true, StatusCode.OK,"添加购物车信息成功",map);
    }

}
